import { takeEvery, put } from 'redux-saga/effects'
import axios from 'axios'
import { load_todo, load_todo_success, add_todo, 
  add_todo_success, clear_todo_completed_success, 
  clear_todo_completed, motify_todo, remove_todo,
  remove_todo_success, motify_todo_success, 
  modify_todo_edit, modify_todo_eidt_success,
  modify_todo_name, modify_todo_name_success } from '../actions/todo.actions'
// 获取数据，传递新指令
function* load_todo_data() {
  let todoData = yield axios.get('/getTodoList').then(res => res.data)
  yield put(load_todo_success(todoData))
}

// 获取数据，传递新指令
function* add_todo_data(action) {
  // console.log(action, '======================')  
  let todoData = yield axios.get('/addList?name='+action.payload).then(res => res.data)
  console.log(todoData, 'todoData')
  yield put(add_todo_success(todoData))
}

// removeTodo
function* remove_todo_data(action) {
  yield axios.get('/delTodoList?id='+action.payload).then(res => res.data)
  let todoData = yield axios.get('/getTodoList').then(res => res.data)
  console.log(todoData, 'todoData')
  yield put(remove_todo_success(todoData))
}

// 获取数据，传递新指令
function* motify_todo_data(action) {
  console.log(action)
<<<<<<< HEAD
  
  yield put(add_todo_success())
=======
  let data =  yield axios.get('/isCompleted?id='+action.payload.id+"&status="+action.payload.status).then(res => res.data)
  console.log(action, data, '======================')
  yield put(motify_todo_success(data))
}

function* clear_todo_completed_data(action) {
  yield axios.get('/clearCompleted')
  yield put(clear_todo_completed_success())
}

function* modify_todo_edit_data(action) {
  console.log(action)
  yield axios.get('/isEditing?id='+ action.payload.id + '&status=' + action.payload.status)
  yield put(modify_todo_eidt_success(action.payload))
}

//  modify_todo_name_data
function* modify_todo_name_data(action) {
  yield axios.get('/updateTaskName?id='+ action.payload.id + '&taskName=' + action.payload.taskName)
  yield put(modify_todo_name_success(action.payload))
>>>>>>> 5b41194010f706041ab6569de1954d8aec763e2f
}

export default function* todoSaga() {
  yield takeEvery(load_todo, load_todo_data)
  yield takeEvery(add_todo, add_todo_data)
  yield takeEvery(remove_todo, remove_todo_data)
  yield takeEvery(motify_todo, motify_todo_data)
  yield takeEvery(clear_todo_completed, clear_todo_completed_data)
  yield takeEvery(modify_todo_edit, modify_todo_edit_data)
  yield takeEvery(modify_todo_name, modify_todo_name_data)
}

/**
 * 1、完成异步操作
 * 2、重新发送新的指令
 */